// #include "util.hpp"
// #include "level.hpp"
// #include "message.hpp"
// #include "format.hpp"
// #include "sink.hpp"
// #include "logger.hpp"
// #include "buffer.hpp"
#include "../logs/log.h"

void test_log()
{
    // log::Logger::ptr logger = log::LoggerManager::getInstance().getLogger("async_logger");
    // logger->DEBUG("%s", "测试");
    // logger->INFO("%s", "测试");
    // logger->WARNNING("%s", "测试");
    // logger->ERROR("%s", "测试");
    // logger->FATAL("%s", "测试");

    DEBUG_LOGOUT("%s", "测试");
    INFO_LOGOUT("%s", "测试");
    WARNNING_LOGOUT("%s", "测试");
    ERROR_LOGOUT("%s", "测试");
    FATAL_LOGOUT("%s", "测试");

    int count = 0;
    while (count < 500000)
    {
        FATAL_LOGOUT("测试-%d", ++count);
    }
}

int main()
{
    // std::string logger_name = "sync_logger";
    // log::Level limit = log::WARNNING;
    // log::Format::ptr fmt(new log::Format("[%d{%H:%M:%S}][%t][%p][%c][%f:%l] %m%n"));
    // log::LogMsg msg(53, "logger", "main.cpp", "测试测试...", log::Level::DEBUG);
    // log::Format fmt;
    // std::string str = fmt->format(msg);
    // log::Sink::ptr std_lsp = log::SinkFactory::create<log::StdOutSink>();
    // log::Sink::ptr file_lsp = log::SinkFactory::create<log::FileSink>("./file.log");
    // log::Sink::ptr roll_lsp = log::SinkFactory::create<log::RollSink>("./roll/roll-", 1024 * 1024);
    // std::vector<log::Sink::ptr> sinks = {std_lsp, file_lsp, roll_lsp};
    // log::Logger::ptr logger(new log::SyncLogger(logger_name, fmt, limit, sinks));
    // logger->debug(__FILE__, __LINE__, "%s", "测试");
    // logger->info(__FILE__, __LINE__, "%s", "测试");
    // logger->warnning(__FILE__, __LINE__, "%s", "测试");
    // logger->error(__FILE__, __LINE__, "%s", "测试");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试");
    // std_lsp->logOut(str.c_str(), str.size());
    // file_lsp->logOut(str.c_str(), str.size());
    // roll_lsp->logOut(str.c_str(), str.size());

    // std::unique_ptr<log::LoggerBuilder> builder(new log::LocalLoggerBuiler());
    // builder->BuilderType(log::SYNC_LOGGER);
    // builder->BuilderName("sync_logger");
    // builder->BuilderLevel(log::DEBUG);
    // builder->BuilderFormat("[%d{%H:%M:%S}][%t][%p][%c][%f:%l] %m%n");
    // builder->BuilderSink<log::StdOutSink>();
    // builder->BuilderSink<log::FileSink>("./sync_file.log");
    // builder->BuilderSink<log::RollSink>("./roll/roll-sync_file-", 1024 * 1024);
    // log::Logger::ptr logger = builder->Build();

    // logger->debug(__FILE__, __LINE__, "%s", "测试");
    // logger->info(__FILE__, __LINE__, "%s", "测试");
    // logger->warnning(__FILE__, __LINE__, "%s", "测试");
    // logger->error(__FILE__, __LINE__, "%s", "测试");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试");

    // int count = 0;
    // int curSize = 0;
    // while (curSize < 1024 * 1024 * 10)
    // {
    //     std::string tmp = "测试-" + std::to_string(count++);
    //     curSize += logger->fatal(__FILE__, __LINE__, "%s", tmp.c_str());
    //     //roll_lsp->logOut(tmp.c_str(), tmp.size());
    // }
    // time_t oldTime = log::Date::curTime();
    // while(log::Date::curTime() < oldTime + 5)
    // {
    //     time_lsp->logOut(str.c_str(), str.size());
    // }

    // std::ifstream ifs("./sync_file.log", std::ios::binary); //以二进制的形式打开文件
    // if(ifs.is_open() == false)
    // {
    //     std::cout << "std::ifstream ifs is_open() is failed" << std::endl;
    //     return -1;
    // }
    // ifs.seekg(0, std::ios::end); //ifs的读写位置跳转到指向文件的末尾
    // int fsize = ifs.tellg(); //获取当前读写位置到begin的大小
    // ifs.seekg(0, std::ios::beg); //ifs的读写位置重现跳转到指向文件的开始
    // std::string msg;
    // msg.resize(fsize);
    // ifs.read(&msg[0], fsize); //读取文件到msg
    // if(ifs.good() == false)
    // {
    //     std::cout << "ifs.read(&msg[0], fsize) is failed" << std::endl;
    //     return -1;
    // }
    // ifs.close(); //关闭文件
    // log::Buffer buffer;
    // //向buffer每次写入一个字符
    // for(int i = 0; i < msg.size(); ++i)
    // {
    //     buffer.push(&msg[i], 1);
    // }
    // std::ofstream ofs("./test_file.log", std::ios::binary); //ofs指向./test_file.log
    // // //一次性把buffer的数据全部写入./test_file.log
    // // ofs.write(buffer.begin(), buffer.readAbleLen());
    // //每次从buffer向./test_file.log写入一个字符
    // int n = buffer.readAbleLen();
    // for(int i = 0; i < n; ++i)
    // {
    //     ofs.write(buffer.begin(), 1);
    //     buffer.moveRead(1); //buffer的可读位置偏移
    // }

    std::unique_ptr<log::LoggerBuilder> builder(new log::GlobalLoggerBuiler());
    builder->BuilderType(log::ASYNC_LOGGER);
    builder->BuilderName("async_logger");
    builder->BuilderLevel(log::DEBUG);
    builder->BuilderEnableUnsafeAsync();
    builder->BuilderFormat("[%c]%m%n");
    builder->BuilderSink<log::StdOutSink>();
    builder->BuilderSink<log::FileSink>("./async_file.log");
    //builder->BuilderSink<log::RollSink>("./roll/roll-async_file-", 1024 * 1024);
    //log::Logger::ptr logger = builder->Build();
    builder->Build();

    test_log();
    // logger->debug(__FILE__, __LINE__, "%s", "测试");
    // logger->info(__FILE__, __LINE__, "%s", "测试");
    // logger->warnning(__FILE__, __LINE__, "%s", "测试");
    // logger->error(__FILE__, __LINE__, "%s", "测试");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试");

    // int count = 0;
    // int curSize = 0;
    // while (curSize < 1024 * 1024 * 10)
    // {
    //     // std::string tmp = "测试-" + std::to_string(count++);
    //     // curSize += logger->fatal(__FILE__, __LINE__, "%s", tmp.c_str());
    //     logger->fatal(__FILE__, __LINE__, "测试-%d", count++);
    //     curSize += 20;
    //     // roll_lsp->logOut(tmp.c_str(), tmp.size());
    // }

    // int count = 0;
    // while (count < 500000)
    // {
    //     logger->fatal(__FILE__, __LINE__, "测试-%d", ++count);
    // }
    return 0;
}